package test230604;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * @date 2023/6/4 16:55
 * 228. 汇总区间
 * https://leetcode.cn/problems/summary-ranges/
 */
public class Solution02 {

    public List<String> summaryRanges(int[] nums) {
        // 用于划分区间, 左闭右开
        int left = 0;
        int right = 0;
        List<String> res = new ArrayList<>();
        while (right < nums.length) {
            right = left + 1;
            StringBuilder temp = new StringBuilder();  // 用于存储每轮的结果
            temp.append(String.valueOf(nums[left]));
            // 找到右区间
            while (right < nums.length && nums[right] - 1 == nums[right - 1]) {
                ++right;
            }
            // 如果有变化
            if (right - 1 != left) {
                temp.append("->" + String.valueOf(nums[right - 1]));
            }
            res.add(temp.toString());
            left = right;
        }
        return res;
    }
}
